package za.co.dylanjaneke.airdownloader.command
{
	import za.co.dylanjaneke.airdownloader.controller.data.ConnectionManager;
	import za.co.dylanjaneke.airdownloader.events.DeleteDownloadSiteEvent;
	import za.co.dylanjaneke.airdownloader.model.SettingsScreenPM;
	import za.co.dylanjaneke.airdownloader.model.TrackerSettingsScreenPM;
	import za.co.dylanjaneke.airdownloader.model.vo.DownloadSiteVO;

	/**
	 * Author: Dylan Janeke.
	 */
	[Name("deleteDownloadSiteCommand")]
	public class DeleteDownloadSiteCommand
	{
		[Inject]
		public var repo:ConnectionManager;
		
		[Inject]
		public var settingsScreenPM:TrackerSettingsScreenPM;
		
		[Observer]
		public function handleDeleteDownloadSiteEvent(event:DeleteDownloadSiteEvent): void
		{
			repo.executeStatement(DownloadSiteVO.createDelete(event.vo, 
				repo.createStatement()));
			
			var i:int=0;
			for each(var dbvo:DownloadSiteVO in settingsScreenPM.sites)
			{
				if(dbvo.url == event.vo.url)
				{
					settingsScreenPM.sites.removeItemAt(i);
				}
				i++;
			}
		}
	}
}